package com.bdqn.ch01.SZ.sz4;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * SimpleBeanConvertor
 *  工具类 添加转换方法
 * @aurhor Administrator  whs
 * @since 2024/7/1
 */
public class SimpleBeanConvertor {

//    private static Logger log = Logger.getLogger(SimpleBeanConvertor.class);

    /**
     *
     * @param requeat  Http 请求
     * @param targetType 目标 JavaBean类型
     * @return  targetType类型的实例
     * @param <T>
     */
    public static <T> T convert(HttpServletRequest requeat , Class<T> targetType) throws IllegalAccessError, InstantiationException, ParseException, InvocationTargetException,UnsupportedTypeException, IllegalAccessException {

    // 获取所有参数请求
        Map<String,String[]> params=requeat.getParameterMap();
        if (params.size()==0) return null;
        T target=targetType.newInstance();
        Method[] allMethods=targetType.getMethods();
        if (allMethods!=null && allMethods.length>0){
            for (Method method:allMethods){
                if (method.getName().startsWith("set")){
                    Class[] args=method.getParameterTypes();
                    if (args.length==1){
                        String paramName=method.getName().substring(3,4).toLowerCase()+method.getName().substring(4);
                        if (params.containsKey(paramName)){
                            try{
                                Object value=parseValue(params.get(paramName),args[0]);
                                method.invoke(target,value);
                            }catch (ParseException e){
                                e.printStackTrace();
//                                log.debug("参数转换错误",e);
                                throw e;
                            }
                        }else if (Boolean.class==args[0]||boolean.class==args[0]){
                            method.invoke(target,false);
                        }
                    }
                }
            }
            return target;
        }

       return null;
    }
    /**
     *  判断参数的类别
     * @param value Http 请求参数值
     * @param type 属性类型
     * @return
     * @throws ParseException
     */
    private static Object parseValue(String[] value,Class type)throws ParseException,UnsupportedTypeException{
        if (String.class==type){
            return value[0];
        }
        if (java.util.Date.class==type){
            return new SimpleDateFormat("yyyy-MM-dd").parse(value[0]);
        }
        if (boolean.class==type || Boolean.class==type){
            return true;
        }
        if (char.class==type||Character.class==type){
            return value[0].charAt(0);
        }
        if (int.class==type||Integer.class==type){
            return Integer.valueOf(value[0]);
        }
        if (float.class == type||Float.class==type){
            return Float.valueOf(value[0]);
        }
        if (short.class==type||byte.class==type||long.class==type||float.class==type||double.class==type){
            try {
                type=Class.forName("java.lang."+type.getName().substring(0,1).toUpperCase()+type.getName().substring(1));
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }
            if (type.getName().startsWith("java.lang.")||type.getName().startsWith("java.math.")){
                try {
                    return type.getConstructor(String.class).newInstance(value[0]);
                }catch (Exception e){
                    throw new UnsupportedTypeException(e);
                }
            }
        }
        throw new UnsupportedTypeException();
    }

}

